package com.huoxingy.statics.repo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by lihu on 2018/1/10.
 */
@Service
public class TotalMemberConsumptionRepoImpl implements TotalMemberConsumptionRepo {

    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public Long queryAllByStoreIdAndBizDate(String storeId, String date) {
        String sql = "select COUNT(*) from total_member_consumption WHERE store_id=? and biz_date=?";
        return jdbcTemplate.queryForObject(sql, Long.class, storeId, date);
    }

    @Override
    public List<Map<String, Object>> findByStoreIdAndBizDate(String storeId, String date, Pageable pageable) {
        String sql = "select member_id memberId,order_count OrderCount,amount" +
                " from total_member_consumption WHERE store_id=? and biz_date=? order by amount desc limit ?,?";
        return jdbcTemplate.queryForList(sql, storeId, date, pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize());
    }

    public List<Map<String, Object>> queryLostInfo(String storeId, String date, Pageable pageable) {

        String sql = "SELECT  b.member_id memberId,b.order_count OrderCount,b.amount FROM `daily_member_lost` a LEFT JOIN  `total_member_consumption` b " +
                " on(a.`member_id`=b.`member_id`)  \n" +
                " WHERE  a.`store_id` =? AND a.`biz_date` =? and  a.`is_lost` =1   " +
                "   AND b.`store_id`=? and b.`biz_date`=?  ORDER BY b.`amount`  DESC limit ?,?";
        return jdbcTemplate.queryForList(sql, storeId,
                date, storeId, date, pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize());
    }
}
